#!/usr/bin/env bash

cd Analyser
rm -rf cache

MITM_FILE="$(mktemp /tmp/mitmlog.XXXXXXXXX)"
MITM_PORT=$((((RANDOM<<15)|RANDOM) % 63001 + 2000 ))

echo "" > "${MITM_FILE}"

mitmdump --ssl-insecure -p "${MITM_PORT}" -s "node_modules/jalangi2/scripts/proxy.py" --set args="--inlineIID --inlineSource --analysis bin/bundle.js" &> "${MITM_FILE}" &

MITM_PID="$!"

while true;
do

	#Test for error 
	if grep -q "Error" "${MITM_FILE}"; then
		echo "Error in mitmproxy"
		cat "${MITM_FILE}"
		unset MITM_PID;
		unset MITM_FILE;
		break;
	fi

	if grep -q "Proxy server listening" "${MITM_FILE}"; then
		break;
	fi

done 

cd ..
